package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.GetBathRet
import com.kiln.juxingqs.api.bean.QueryUser
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.Response

class ZrxyViewModel(val app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val queryRet = MutableLiveData<JsonResult<QueryUser>>()
    val isShowing = MutableLiveData<Boolean>()

    val sNum = MutableLiveData<String>("")
    val otherAccount = MutableLiveData<String>("")
    val password = MutableLiveData<String>("")
    val otherName = MutableLiveData<String>("")
    val selectBath = MutableLiveData<GetBathRet.Bath>()
    val price = MutableLiveData<String>("")
    val goodsId = MutableLiveData<String>("")
//    val selectDddBath = MutableLiveData<GetDddBathRet.Bath>()

    val guaMaiWho = MutableLiveData<String>("")
    val serverName = MutableLiveData<String>("")


    /**
     */
    fun ok() {
        if (TextUtils.isEmpty(goodsId.value)) {
            toast("商品id为空")
            return
        }
        if (TextUtils.isEmpty(sNum.value)
        ) {
            toast("请输入交易数量")
            return
        }
        if (TextUtils.isEmpty(otherAccount.value)
        ) {
            toast("请输入对方账号")
            return
        }
        if (TextUtils.isEmpty(otherName.value)
        ) {
            toast("对方姓名不能为空")
            return
        }
        if (TextUtils.isEmpty(password.value)
        ) {
            toast("请输入交易密码")
            return
        }
        if (price.value.isNullOrEmpty() || price.value.equals("0")) {
            toast("请输入交易价格")
            return
        }

        isShowing.value = true
        viewModelScope.launch {
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(password.value!!)
                    emm = ret
                }
                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        var payType = "1"
                        out_time {
                            val ret = server.seller_transfer_order(
                                otherAccount.value!!,
                                sNum.value!!,
                                goodsId.value!!,
//                                "${selectBath.value?.bath_id}",
                                price.value!!
                            )
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.value = false
                }.catch { cause ->
                    if (cause != null) {
                        toast(cause.message)
                    }
                }.collectLatest {
                    call.value = it?.body()

                }
        }
    }

    fun query() {
        if (otherAccount.value.isNullOrEmpty()) {
            toast("请输入内容")
            return
        }
        hold({
            server.queryUser(otherAccount.value!!)
        }, {
            isShowing.value = true
        }, {
            queryRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }
}